#include "SpriteEasing.h"


SpriteEasing::SpriteEasing()
{
	//tween::Bounce
}

SpriteEasing::SpriteEasing(tween::kindEasing kindAnim, tween::easingFunction kindFunc, long duration)
	: param(duration, kindAnim, kindFunc)
	, kindAnim(kindAnim)
	, kindFunc(kindFunc)
	, endValueX(endValueX)
	, endValueY(endValueY)
	, duration(duration)
	, animating(false)
	, linkSprite(nullptr)
	, currentTime(0.0f)
	, canStop(false)
	, theSecondTween(nullptr)
	, secondEndValueX(0.0f)
	, secondEndValueY(0.0f)
	, scaleMultiplicator(0.0f)
	,isScale(false)
{
}


SpriteEasing::~SpriteEasing()
{

}

void SpriteEasing::StartTweenAnim(SDLSprite* linkSprite, float endValueX, float endValueY)
{
	this->linkSprite = linkSprite;
	posX = (float)linkSprite->GetX();
	posY = (float)linkSprite->GetY();

	tweener.ResetLastTime();

	param.cleanProperties();
	//tweener.removeTween(&param);

	param.addProperty(&posX, endValueX);
    param.addProperty(&posY, endValueY);
	tweener.addTween(param);

	animating = true;
	currentTime = 0.0f;

	canStop = false;
}

void SpriteEasing::StartTweenAnimScale(SDLSprite* linkSprite, float beginMultiplicator, float endMultiplicator)
{
	this->linkSprite = linkSprite;
	linkSprite->calculeCenterPoint();
	scaleMultiplicator = beginMultiplicator;

	tweener.ResetLastTime();

	param.cleanProperties();
	//tweener.removeTween(&param);

	param.addProperty(&scaleMultiplicator, endMultiplicator);
	tweener.addTween(param);

	animating = true;
	isScale = true;
	currentTime = 0.0f;

	canStop = false;
}


void SpriteEasing::StartTweenAnim(SpriteEasing* secondTween, SDLSprite* linkSprite, float endValueX, float endValueY, float nextEndValueX, float nextEndValueY)
{
	this->linkSprite = linkSprite;
	posX = (float)linkSprite->GetX();
	posY = (float)linkSprite->GetY();

	tweener.ResetLastTime();

	param.cleanProperties();
	//tweener.removeTween(&param);

	param.addProperty(&posX, endValueX);
    param.addProperty(&posY, endValueY);
	tweener.addTween(param);

	animating = true;
	currentTime = 0.0f;

	theSecondTween = secondTween;
	secondEndValueX = nextEndValueX;
	secondEndValueY = nextEndValueY;

	canStop = false;
}


void SpriteEasing::StopTweenAnim()
{
	animating = false;
	isScale = false;
	currentTime = 0.0f;
}


 void SpriteEasing::Update()
 {
	if(animating == true)
	{
		currentTime += SDLTimer::GetInstance()->GetDeltaTime();

		long theCurrentTime = (long)(currentTime * 1000);

		if(theCurrentTime <= duration)
		{
			tweener.step(theCurrentTime);

			if(isScale == false)
				linkSprite->SetPosition((int)posX, (int)posY);
			else
				linkSprite->ScaleBy(scaleMultiplicator);

			
			canStop = true;
		}
		else if(canStop == true)
		{
			StopTweenAnim();
			currentTime = 0.0f;

			if(theSecondTween != nullptr)
			{
				theSecondTween->StartTweenAnim(linkSprite, secondEndValueX, secondEndValueY);
			}
		}
		else
		{
			currentTime = 0.0f;
		}
	}
 }